<?php

/**
 * @Project NUKEVIET 3.x
 * @Author VINADES.,JSC (contact@vinades.vn)
 * @Copyright (C) 2012 VINADES.,JSC. All rights reserved
 * @Createdate 2-9-2010 14:43
 */

if( ! defined( 'NV_IS_FILE_ADMIN' ) ) die( 'Stop!!!' );

$page_title = $lang_module['block'];
$set_active_op = "blockcat";

$sql = "SELECT `bid`, `title` FROM `" . NV_PREFIXLANG . "_" . $module_data . "_block_cat` ORDER BY `weight` ASC";
$result = $db->sql_query( $sql );

if( $db->sql_numrows( $result ) )
{
	$array_block = array();
	while( list( $bid_i, $title_i ) = $db->sql_fetchrow( $result ) )
	{
		$array_block[$bid_i] = $title_i;
	}
}
else
{
	Header( "Location: " . NV_BASE_ADMINURL . "index.php?" . NV_NAME_VARIABLE . "=" . $module_name . "&" . NV_OP_VARIABLE . "=blockcat" );
}

$cookie_bid = $nv_Request->get_int( 'int_bid', 'cookie', 0 );
if( empty( $cookie_bid ) or ! isset( $array_block[$cookie_bid] ) )
{
	$cookie_bid = 0;
}

$bid = $nv_Request->get_int( 'bid', 'get,post', $cookie_bid );
if( ! in_array( $bid, array_keys( $array_block ) ) )
{
	$bid_array_id = array_keys( $array_block );
	$bid = $bid_array_id[0];
}

if( $cookie_bid != $bid )
{
	$nv_Request->set_Cookie( 'int_bid', $bid, NV_LIVE_COOKIE_TIME );
}
$page_title = $array_block[$bid];

if( $nv_Request->isset_request( 'checkss,idcheck', 'post' ) and $nv_Request->get_string( 'checkss', 'post' ) == md5( session_id() ) )
{
	$id_array = array_map( "intval", $nv_Request->get_array( 'idcheck', 'post' ) );
	foreach( $id_array as $id )
	{
		$db->sql_query( "INSERT INTO `" . NV_PREFIXLANG . "_" . $module_data . "_block` (`bid`, `id`, `weight`) VALUES ('" . $bid . "', '" . $id . "', '0')" );
	}
	nv_news_fix_block( $bid );
	nv_del_moduleCache( $module_name );
	Header( "Location: " . NV_BASE_ADMINURL . "index.php?" . NV_NAME_VARIABLE . "=" . $module_name . "&" . NV_OP_VARIABLE . "=" . $op . "&bid=" . $bid );
	die();
}

$select_options = array();
foreach( $array_block as $xbid => $blockname )
{
	$select_options[NV_BASE_ADMINURL . "index.php?" . NV_NAME_VARIABLE . "=" . $module_name . "&amp;" . NV_OP_VARIABLE . "=" . $op . "&amp;bid=" . $xbid] = $blockname;
}

$xtpl = new XTemplate( "block.tpl", NV_ROOTDIR . "/themes/" . $global_config['module_theme'] . "/modules/" . $module_file );
$xtpl->assign( 'LANG', $lang_module );
$xtpl->assign( 'GLANG', $lang_global );
$xtpl->assign( 'NV_BASE_ADMINURL', NV_BASE_ADMINURL );
$xtpl->assign( 'NV_NAME_VARIABLE', NV_NAME_VARIABLE );
$xtpl->assign( 'NV_OP_VARIABLE', NV_OP_VARIABLE );
$xtpl->assign( 'MODULE_NAME', $module_name );
$xtpl->assign( 'OP', $op );
$xtpl->assign( 'BLOCK_LIST', nv_show_block_list( $bid ) );

$id_array = array();
$listid = $nv_Request->get_string( 'listid', 'get', '' );
if( $listid == "" )
{
	$sql = "SELECT id, title FROM `" . NV_PREFIXLANG . "_" . $module_data . "_rows` where `status`=1 AND `id` NOT IN(SELECT `id` FROM `" . NV_PREFIXLANG . "_" . $module_data . "_block` WHERE `bid`=" . $bid . ") ORDER BY `publtime` DESC LIMIT 0,20";
}
else
{
	$id_array = array_map( "intval", explode( ",", $listid ) );
	$sql = "SELECT id, title FROM `" . NV_PREFIXLANG . "_" . $module_data . "_rows` where `status`=1 AND `id` IN (" . implode( ",", $id_array ) . ") ORDER BY `publtime` DESC";
}

$result = $db->sql_query( $sql );
if( $db->sql_numrows( $result ) )
{
	$a = 0;
	while( list( $id, $title ) = $db->sql_fetchrow( $result ) )
	{		
		$xtpl->assign( 'ROW', array(
			"class" => ( $a % 2 ) ? " class=\"second\"" : "",
			"checked" => in_array( $id, $id_array ) ? " checked=\"checked\"" : "",
			"title" => $title,
			"id" => $id,
		) );
		
		$xtpl->parse( 'main.news.loop' );
		++ $a;
	}

	foreach( $array_block as $xbid => $blockname )
	{
		$xtpl->assign( 'BID', array( "key" => $xbid, "title" => $blockname, "selected" => $xbid == $bid ? " selected=\"selected\"" : "" ) );
		$xtpl->parse( 'main.news.bid' );
	}
	
	$xtpl->assign( 'CHECKSESS', md5( session_id() ) );
	$xtpl->parse( 'main.news' );
}

$xtpl->parse( 'main' );
$contents = $xtpl->text( 'main' );

include ( NV_ROOTDIR . "/includes/header.php" );
echo nv_admin_theme( $contents );
include ( NV_ROOTDIR . "/includes/footer.php" );

?>